﻿using System;
using System.Collections;
using System.Collections.Generic;
namespace Sample
{
	class MpJsDataLoader
	{
		MpJsDataLoader()
		{ }

		static MpJsDataLoader()
		{
			Singleton=new MpJsDataLoader();
		}

		public static MpJsDataLoader Singleton { get; private set; }

		public ResultSets getData()
		{
			ResultSets result=new ResultSets();

			//generuj produkty
			ResultSet dataGood=new ResultSet() { Fields=new string[] { "mc.id","name" } };
			result.Add(dataGood);
			for (int a=1;a<21;a++)
			{
				ResultSetRow row=dataGood.AddRow();
				row[0]=a;
				row[1]="Product "+a.ToString()+" - name";
			}

			//generuj zarazeni do kategorii
			ResultSet dataGood2Category=new ResultSet() { Fields=new string[] { "id_good","id_category" } };
			result.Add(dataGood2Category);
			int[][] tempPairs=new int[][]
			{
				new int[]{1},
				new int[]{2,3},
				new int[]{4,5},
				new int[]{1,6},
				new int[]{9},
				new int[]{7,8},
				new int[]{3,5,9},
				new int[]{3,5,8},
				new int[]{1,4},
				new int[]{5,8},
				new int[]{2,6},
				new int[]{1,2,3,4,5,6,7,8,9},
				new int[]{3,4},
				new int[]{7,8},
				new int[]{2,5,6},
				new int[]{1,2,4},
				new int[]{6,8},
				new int[]{3,9},
				new int[]{2,9},
				new int[]{1,2,3}
			};
			for (int a=0;a<20;a++)
				for (int b=0;b<tempPairs[a].Length;b++)
				{
					ResultSetRow row=dataGood2Category.AddRow();
					row[0]=a+1;
					row[1]=tempPairs[a][b];
				}

			//generuj kategorie
			ResultSet dataCategory=new ResultSet() { Fields=new string[] { "mc.id","name" } };
			result.Add(dataCategory);
			for (int a=1;a<10;a++)
			{
				ResultSetRow row=dataCategory.AddRow();
				row[0]=a;
				row[1]="Category "+a.ToString()+" - name";
			}

			return result;
		}

		public Dictionary<object,object> createIndex(ResultSet list,Func<object,object> indexFunc,Func<object,object> itemFunc)
		{
			Dictionary<object,object> result=new Dictionary<object,object>();
			int len=list.Count;
			for (var a=0;a<len;a++)
			{
				object item=list[a];
				result.Add(indexFunc(item),itemFunc(item));
			}
			return result;
		}

		public Dictionary<object,List<object>> createGroup(ResultSet list,Func<object,object> indexFunc,Func<object,object> itemFunc)
		{
			Dictionary<object,List<object>> result=new Dictionary<object,List<object>>();
			int len=list.Count;
			for (var a=0;a<len;a++)
			{
				object item=list[a];
				object indexValue=indexFunc(item);
				object value=itemFunc(item);
				List<object> temp;
				if (!result.TryGetValue(indexValue,out temp))
				{
					temp=new List<object>();
					result.Add(indexValue,temp);
				}
				temp.Add(value);
			}
			return result;
		}
	}

	#region result set objects
	public class ResultSet:List<ResultSetRow>
	{
		public string[] Fields { get; set; }

		internal ResultSetRow AddRow()
		{
			ResultSetRow result=new ResultSetRow(this);
			this.Add(result);
			return result;
		}
	}

	public class ResultSets:List<ResultSet>
	{ }

	public class ResultSetRow:ArrayList
	{
		ResultSet _resultSet;
		internal ResultSetRow(ResultSet resultSet)
			: base(resultSet.Fields.Length)
		{
			_resultSet=resultSet;

			for (int a=0;a<resultSet.Fields.Length;a++)
				this.Add(null);
		}

		int GetFieldIndex(string fieldName)
		{
			int result=Array.IndexOf(_resultSet.Fields,fieldName);
			if (result==-1)
				throw new IndexOutOfRangeException();
			return result;
		}

		public object this[string fieldName]
		{
			get { return this[GetFieldIndex(fieldName)]; }
			set { this[GetFieldIndex(fieldName)]=value; }
		}
	}
	#endregion result set objects
}